package com.moter.demo01.juc.blockingqueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class ArrayBlockingQueueDemo {
    public static void main(String[] args) {
        try {
            test4();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //抛异常的api,取和放
    public static void test1(){
        BlockingQueue<String>  blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.add("a");
        blockingQueue.add("a");
        blockingQueue.add("a");
        //如果加入的元素超过了容量 抛出java.lang.IllegalStateException: Queue full,队列满异常
//        blockingQueue.add("a");
        System.out.println("======================");
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.element());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        //如果队列为空再去取,就会抛出NoSuchElementException
//        System.out.println(blockingQueue.element());
//        System.out.println(blockingQueue.remove());
    }
    //不会抛出异常,根据boolean值(放)和null(取)判断是否放和取成功
    public static void test2(){
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);

        System.out.println(queue.offer("a"));
        System.out.println(queue.offer("a"));
        System.out.println(queue.offer("a"));
        //如果超过容量大小,返回false
        System.out.println(queue.offer("a"));

        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        //如果队列为空,返回null
        System.out.println(queue.peek());
        System.out.println(queue.poll());
    }
    //不会抛出异常,没有返回值,会进行阻塞,直到可以取,或者可以放为止,
    public static void test3() throws InterruptedException {
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
        queue.put("a");
        queue.put("b");
        queue.put("c");
        //如果容量超过,会一直等,直到有元素被取出来,它才会放进去
        //换成线程执行得话,也可以很清晰得出结论
        new Thread(()->{
            try {

                System.out.println(queue.take());

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
                queue.put("d");

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        System.out.println(123);
        System.out.println(queue.take());
        System.out.println(queue.take());
        System.out.println(queue.take());
        //如果队列为空,会一直等,直到有元素放进去,它会立马取出来.
//        System.out.println(queue.take());
    }
    //可以设置时间的超时等待.
    public static void test4() throws InterruptedException {
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
        System.out.println(queue.offer("a"));
        System.out.println(queue.offer("a"));

        //如果超过容量大小,返回false
        System.out.println(queue.offer("a",2,TimeUnit.SECONDS));


        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll(2,TimeUnit.SECONDS)+"123");
        //如果队列为空,返回null
        System.out.println(queue.peek());


    }
}
